package com.cookie; /**
 * @author wzm
 * @create 2023-03-13 11:23
 */

import com.utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cookie02")
public class CookieServlet02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     response.setContentType("text/html;charset=utf-8");
    //测试Cookie是不是每次请求服务器都会发送到服务器中
        
        //获取当前Cookie 数组
        Cookie[] cookies = request.getCookies();
        //遍历
      /*  for (Cookie cookie : cookies) {
            //获取cookie的key
            String name = cookie.getName();
            if("username".equals(name)){
                //那么此时取出该cookie的值就是我们想要的
                String value = cookie.getValue();
                System.out.println(value);
                return;
            }

        }


*/

        Cookie username = CookieUtils.getCookie("username", cookies);
        if(username != null){
            System.out.println("找到了cookie:"+username.getValue());
        }


    }
}
